package com.qf.controller.manager;

import com.github.pagehelper.PageInfo;
import com.qf.entitys.MerchantClassification;
import com.qf.entitys.RespBean;
import com.qf.service.MerchantClassificationService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Random;

@Controller
@RequestMapping("/manager/category")
public class MerchantClassificationController {

    @Resource
    private MerchantClassificationService service;

    @GetMapping("/list")
    public String list(Integer pageindex, HttpServletRequest request){
        PageInfo<MerchantClassification> pageInfo = service.selectAll(pageindex);
        request.setAttribute("pageInfo",pageInfo);
        return "manager/MerchantClassificationList";
    }

    @GetMapping("/add")
    public String add(){
        return "manager/MerchantClassificationAdd";
    }

    @PostMapping("/add")
    public String addCategory(MerchantClassification me, MultipartFile file,HttpServletRequest request) throws IOException {
        if (!file.isEmpty()){
            //获取要存放的真实路径
            String realPath = request.getServletContext().getRealPath("/statics/upload/category");
            //fileupload包里的工具，getExtension用于获取文件名的后缀
            String s = FilenameUtils.getExtension(file.getOriginalFilename());
            String filename=System.currentTimeMillis()+"_"+new Random().nextInt(10000)+s;
            //创建file文件对象
            File file1 = new File(realPath, filename);
            //上传文件
            file.transferTo(file1);
            me.setMcIcon(filename);
            int i = service.insertSelective(me);
            if (i>0){
                return "redirect:list?pageindex=1";
            }else {
                return "manager/systemerror";
            }
        }
        return "manager/systemerror";
    }

    @GetMapping("/del")
    @ResponseBody
    public RespBean delById(Integer id){
        try {
            int i = service.deleteByPrimaryKey(id);
            if (i>0){
                return RespBean.success("删除成功");
            }else {
                return RespBean.error("删除失败");
            }
        }catch (Exception ex){
            return RespBean.error("删除失败");
        }
    }

    @GetMapping("/delbyids")
    @ResponseBody
    public RespBean delbyids(@RequestParam("ids[]")Integer[] ids){
        try {
            int i = service.deleteByIds(ids);
            if (i>0){
                return RespBean.success("批量删除成功！");
            }else {
                return RespBean.error("批量删除失败！");
            }
        }catch (Exception ex){
            return RespBean.error("批量删除失败！");
        }
    }

    @GetMapping("/update")
    public String update(Integer id,HttpServletRequest request){
        MerchantClassification merchantClassification = service.selectByPrimaryKey(id);
        request.setAttribute("mc",merchantClassification);
        return "manager/MerchantClassificationUpdate";
    }

    @PostMapping("/update")
    @ResponseBody
    public RespBean update1(MerchantClassification mc,MultipartFile file,HttpServletRequest request) throws IOException {
        if (!file.isEmpty()){
            String realPath = request.getServletContext().getRealPath("/statics/upload/category");
            String extension = FilenameUtils.getExtension(file.getOriginalFilename());
            String filename=System.currentTimeMillis()+"_"+new Random().nextInt(10000)+extension;
            File file1 = new File(realPath, filename);
            file.transferTo(file1);
            mc.setMcIcon(filename);
        }
        int i = service.updateByPrimaryKeySelective(mc);
        if (i>0){
            return RespBean.success("修改成功，即将返回列表!");
        }else {
            return RespBean.error("修改失败!");
        }
    }

}
